package cn.cf.kxx.data;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.fragment.app.FragmentActivity;

import com.google.gson.Gson;

import org.json.JSONObject;

import cn.cf.kxx.model.dev.Hold;
import cn.cf.kxx.tools.Tools;

public class DataSharedPreferences {

    private SharedPreferences.Editor editor;
    private SharedPreferences settings;
    private static DataSharedPreferences instance = null;

    private DataSharedPreferences() {
    };

    public static DataSharedPreferences getInstance() {
        if (instance == null) {
            instance = new DataSharedPreferences();
        }
        return instance;
    }

    public void addUpdateXml(Context context, String xml){
        initShared(context, "xml");
        editor.putString("value", xml);
        editor.putString("time", System.currentTimeMillis()+"");
        editor.commit();
    }

    public String[] getUpdateXml(Context context){
        initShared(context, "xml");
        return new String[]{settings.getString("time", null),settings.getString("value", null)};
    }

    public void addDisplay(Context context, String[] pixels) {
        initShared(context, "display");
        editor.putString("xPixels", pixels[0]);
        editor.putString("yPixels", pixels[1]);
        editor.putString("dPixels", pixels[2]);
        editor.commit();
    }

    public String[] getDisplay(Context context) {
        initShared(context, "display");
        String[] str = new String[] { settings.getString("xPixels", "0"),
                settings.getString("yPixels", "0"),
                settings.getString("dPixels", "0") };
        return str;
    }
    //设备授权token
    public void addUser(Context context, String user){
        initShared(context, "userValue");
        editor.putString("user", user);
        editor.commit();
    }

    public String getUser(Context context){
        initShared(context, "userValue");
        return settings.getString("user",null);
    }

    public void addDevice(Context context, String user){
        initShared(context, "devValue");
        editor.putString("dev", user);
        editor.commit();
    }

    public String getDevice(Context context){
        initShared(context, "devValue");
        return settings.getString("dev",null);
    }

    public void addFarm(Context context, String farm){
        initShared(context, "farmValue");
        editor.putString("farm", farm);
        editor.commit();
    }

    public String getFarm(Context context){
        initShared(context, "farmValue");
        return settings.getString("farm",null);
    }

    public void addWeather(Context context, String farm){
        initShared(context, "WeatherValue");
        editor.putString("Weather", farm);
        editor.commit();
    }

    public String getWeather(Context context){
        initShared(context, "WeatherValue");
        return settings.getString("Weather",null);
    }

    public void addDd(Context context, String json){
        initShared(context,"Dd");
        editor.putString("json",json);
        editor.commit();
    }

    public String getDd(Context context){
        initShared(context, "Dd");
        return settings.getString("json",null);
    }



    /**
     * @param context
     * @param guide
     */
    public void addGuide(Context context, String guide){
        initShared(context, "guideValue");
        editor.putString("value", guide);
        editor.commit();
    }

    public String getGuide(Context context){
        initShared(context, "guideValue");
        return settings.getString("value","[]");
    }

    /**
     * @param context
     * @param guide
     */
    public void addGroup(Context context, String guide){
        initShared(context, "groupValue");
        editor.putString("value", guide);
        editor.commit();
    }

    public String getGroup(Context context){
        initShared(context, "groupValue");
        return settings.getString("value","[]");
    }

    /**
     * @param context
     * @param exeplan
     */
    public void addExePlan(Context context, String exeplan){
        initShared(context, "exeValue");
        editor.putString("value", exeplan);
        editor.commit();
    }

    public String getExePlan(Context context){
        initShared(context, "exeValue");
        return settings.getString("value",null);
    }

    public void addVersion(Context context, String value){
        initShared(context, "version");
        editor.putString("value", value);
        editor.commit();
    }

    public String getVersion(Context context){
        initShared(context, "version");
        return settings.getString("value",null);
    }

    /**
     * 保持寄存器存储
     * @param context
     * @param hold
     */
    public void addHold(Context context, Hold hold){
        initShared(context, "hold");
        editor.putString("value", Tools.gson.toJson(hold));
        editor.commit();
    }

    public Hold getHold(Context context){
        initShared(context, "hold");
        Hold hold = Tools.gson.fromJson(settings.getString("value","{\"id\": 1}"), Tools.getHoldType());
        return hold;
    }




    private void initShared(Context context, String dbname) {
        settings = context.getSharedPreferences(dbname, 0);
        editor = settings.edit();
    }


}
